#ifndef __NIGHTCSV_H__
#define __NIGHTCSV_H__

#include <assert.h>
#include <vector>
#include <list>


namespace NE { namespace CsvParser
{

	const unsigned int STATIC_POOL_SIZE		= 20480;
	const unsigned int DYNAMIC_POOL_SIZE	= 5120;


	class CsvItem
	{
		friend class CsvDocument;
	public:
		CsvItem(void);
		CsvItem(const CsvItem& item);
		CsvItem(const char* str, unsigned int size);

		const char* getText(void) const
		{
			return mString;
		}

		unsigned int getSize(void)
		{
			return mSize;
		}

	protected:
		const char* mString;
		unsigned int mSize;
	};


	typedef std::vector< std::vector<CsvItem*> > CsvItemTable;


	class CsvDocument
	{
	public:
		CsvDocument(void);
		~CsvDocument(void);

		void parse(const char* text);

		const CsvItemTable& getItemTable(void)
		{
			return mItemTable;
		}

		const CsvItem* getItem(unsigned int column, unsigned int row)
		{
			assert(column < mColumnCount && row < mRowCount);
			return mItemTable[column][row];
		}

		CsvItem* allocateItem(const char* text, unsigned int size);

	protected:
		CsvItemTable mItemTable;
		unsigned int mColumnCount;
		unsigned int mRowCount;

	private:
		CsvItem* mFreshItem;
		CsvItem* mEndItem;
		CsvItem mStaticItemPool[STATIC_POOL_SIZE];
		std::list<CsvItem*> mDynamicPools;
	};

}}


#endif
